package com.jielin.handling.third.xsky;

import java.util.List;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.HeadBucketRequest;
import com.amazonaws.services.s3.model.ListMultipartUploadsRequest;
import com.amazonaws.services.s3.model.MultipartUploadListing;

public class XskyS3Bucket 
{
	
	public AmazonS3 client = null;
	
	public XskyS3Bucket(AmazonS3 client) 
	{
		this.client = client;
	}
	
	// 列出当前用户的所有Bucket
	public List<Bucket> ListBuckets() 
	{
		List<Bucket> bucketList = null;
		try {
			bucketList = client.listBuckets();
		}catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException when listBuckets.");
			System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
		}
		return bucketList;
	}
	
	// 检查桶是否存在
	public Boolean HasBucket(String bucketName) 
	{
		Boolean b = false;
		try {
			b = client.doesBucketExistV2(bucketName);
		}catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException when check \""+bucketName+"\".");
			System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
		}
		return b;
	}
	
	// 创建桶
	public Bucket CreatingBucket(String bucketName)
    {
		Bucket bucket = null;
		try {
			bucket = client.createBucket(bucketName);
		}catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException when create \""+bucketName+"\".");
			System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
		}
		return bucket;
    }
	
	// 删除桶
	public void DeletingBucket(String bucketName)
    {
		try {
			client.deleteBucket(bucketName);
		}catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException when delete \""+bucketName+"\".");
			System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
		}
    }
	
	// 检查是否有权限访问桶
	public void HeadBuckte(String bucketName)
	{
		HeadBucketRequest headBucketRequest = new HeadBucketRequest(bucketName);
		try {
			client.headBucket(headBucketRequest);
		}catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException when head \""+bucketName+"\".");
			System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
		}
	}
	
	// 列出分段上传
	public MultipartUploadListing listMultipartUploads(String bucketName) {
		try {
			return client.listMultipartUploads(new ListMultipartUploadsRequest(bucketName));	
		}catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException when list MultipartUploads of \""+bucketName+"\".");
			System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
		}
		return null;
	}
}
